package com.hoshiibuy.sys.permission.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.sys.permission.entity.SysPermissiones;
import com.hoshiibuy.sys.permission.entity.param.SysPermissionesInsertParam;
import com.hoshiibuy.sys.permission.entity.param.SysPermissionesQueryParam;
import com.hoshiibuy.sys.permission.entity.param.SysPermissionesUpdateParam;
import com.hoshiibuy.sys.permission.entity.vo.SysPermissionesVO;
import com.hoshiibuy.sys.permission.service.ISysPermissionesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 权限控制器
 * 
 * @author Mr.wu
 *
 */
@Api(tags = "权限相关接口")
@RestController
@RequestMapping("/permission")
public class PermissionController {

	private final static Logger logger = LoggerFactory.getLogger(PermissionController.class);

	@Autowired
	private ISysPermissionesService sysPermissionesService;

	@ApiOperation(value = "新增一项权限")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response<String> addPermission(@Valid SysPermissionesInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysPermissiones permission = new SysPermissiones();
		BeanUtils.copyProperties(param, permission);
		// 调用内置工具产生ID
		permission.setId(IdWorker.getId());
		permission.setAddTime(LocalDateTime.now());
		// TODO 获取创建人
		permission.setCreatedby("admin");
		// TODO 参数过滤：只接受指定参数
		return sysPermissionesService.addPermission(permission);

	}

	@ApiOperation(value = "修改指定权限")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updatePermission(SysPermissionesUpdateParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysPermissiones permission = new SysPermissiones();
		BeanUtils.copyProperties(param, permission);
		permission.setUpdateTime(LocalDateTime.now());
		return sysPermissionesService.updatePermissionById(permission);
	}

	@ApiOperation(value = "获取一项权限的详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysPermissionesVO> getPermissionDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysPermissionesService.getDetail(id);
	}

	@ApiOperation(value = "获取权限列表")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<SysPermissionesVO>> getPermissionList(SysPermissionesQueryParam param,
			BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysPermissiones> queryWrapper = param.getQueryWrapper();
		queryWrapper.select(BeanUtils.getSqlSelect(SysPermissionesVO.class));
		return sysPermissionesService.getList(queryWrapper);

	}

	@ApiOperation(value = "获取权限树", notes = "获取某项权限的完整权限树,包括自身")
	@RequestMapping(value = "getTree", method = RequestMethod.GET)
	public Response<List<SysPermissionesVO>> getPermissionTree(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		Response isExist = sysPermissionesService.getDetail(id);
		if (!isExist.isSuccess()) {
			return Response.failed("权限不存在");
		}
		return sysPermissionesService.getChildList(id);
	}

	@ApiOperation(value = "删除一项权限", notes = "无法删除权限树，如果有相关联的下级权限，将返回失败。")
	@RequestMapping(value = "/deleteOne", method = RequestMethod.POST)
	public Response deleteOnePermission(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysPermissionesService.deletePermissionById(id);
	}

	@ApiOperation(value = "删除一棵权限树", notes = "将删除与其关联的所有下级权限")
	@RequestMapping(value = "deleteTree", method = RequestMethod.POST)
	public Response deletePermissionTree(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		// TODO 编写sql删除权限树
		return Response.success("接口尚未实现");
	}

	@ApiOperation("分页查询权限")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<SysPermissionesVO> getPermissionPage(@Valid SysPermissionesQueryParam param,
			BindingResult bindingResult) {
		QueryWrapper<SysPermissiones> queryWrapper = param.getQueryWrapper();

		IPage<SysPermissiones> page = new Page<>(Long.parseLong(param.getPageNumber()),
				Long.parseLong(param.getPageSize()));

		return sysPermissionesService.getPage(page, queryWrapper);
	}

}
